import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Platform,
  TextInput,
  TouchableOpacity,
  DeviceEventEmitter,
  NativeAppEventEmitter,
  ScrollView
} from 'react-native';

import Toast from 'react-native-sk-toast';
import api from '../../services/api';
import _ from 'lodash';
import moment from 'moment';
import styles from './inputGoodsPriceStyle';
import ToastTip from '../global/ToastTip';
import LoadingTip from '../global/LoadingTip';
import dismissKeyboard from 'dismissKeyboard';
import * as config from '../../constants/apiConfig';
// import Touch from '../global/Touch';

class InputGoodsPrice extends Component {

    constructor(props) {
      super(props);
      this.state = {
          obj_price: '',
          showLoading: false,
          btnDisabled: false,
      };

    }

    componentWillMount(){

    }

    componentWillUnmount(){
        // let {actions, dispatch} = this.props;
        // await this._createSession();
    }

    async _submitAction(){
        let {order_info, session, navigator} = this.props;
        let {obj_price} = this.state;

        // console.log("@@@", obj_price);

        if(!obj_price){
            this.refs.toast.show('请输入商品金额');
            return;
        }

        if(parseFloat(obj_price) == 0){
            this.refs.toast.show('请输入大于零的金额');
            return;
        }

        this.setState({showLoading: true, btnDisabled: true});

        let ownerInfo = await api.post('/housekeeper/ownerinfobyphone',{phone: order_info.user_phone});
        if(ownerInfo.data){
            let updatenotpay = await api.post('/order/objnotpay',{obj_actual_price: obj_price, id:order_info.id});
            if(updatenotpay.cd == 1){
                this.setState({showLoading: false, btnDisabled: false});
                this.refs.toast.show(updatenotpay.msg);
                return;
            }
            
            if(updatenotpay && updatenotpay.data){
                // let send_text = "";
                // send_text = "管家"+(session && session.user.keeper_name)+"已完成配送，<a href='" + config.WEB_BASE_URL + "/weichart/order?order_id="+order_info.id+"&type=obj&price="+obj_price+"'>请支付商品金额</a>";
                //
                // let json = JSON.stringify({
                //                 "touser":ownerInfo.data.wxOpenId,
                //                 "msgtype":"text",
                //                 "text":
                //                 {
                //                      "content":send_text,
                //                 }
                //                 // "customservice":
                //                 // {
                //                 //     "kf_account": session && session.user.kf_account
                //                 // }
                //             });
                //
                // let send_res = await api.post('/wx/customsend',{json});

                // let {actions, dispatch} = this.props;
                // dispatch(actions.order.order_done_obj_price(order_info.id)).then(()=>{
                //
                // });

                this.setState({showLoading: false});
                let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
                Emitter.emit("order.onload");

                navigator.pop();
            }

        }else{
            this.setState({showLoading: false, btnDisabled: false});
            this.refs.toast.show('输入的手机号未注册，用户无法接收提醒');
        }

    }

    render() {
      let {session, userInfo, message} = this.props;
      if(!session){
          return null;
      }

      return (
          <View style={styles.wrapper} onStartShouldSetResponderCapture={(e) => {
              const focusField = TextInput.State.currentlyFocusedField();
              if (focusField != null && e.nativeEvent.target != focusField){
                  dismissKeyboard();
              }
          }}>
              <ScrollView style={styles.container}>
                  <View style={styles.content}>
                      <Text style={styles.titleStyle}>请输入商品金额</Text>
                      <View style={styles.inputContent}>
                          <Text style={styles.subStyle}>¥</Text>
                          <TextInput
                              style={styles.inputStyle}
                              editable={true}
                              autoFocus={Platform.OS=="ios"?true:false}
                              value={this.state.obj_price}
                              ref={ (component) => this._inputComp = component }
                              keyboardType={'numeric'}
                              clearButtonMode={'while-editing'}
                              underlineColorAndroid={'#ffffff'}
                              placeholder={''}
                              onChangeText={(text)=>{
                                  let {obj_price} = this.state;
                                  if(text && !/^\d+(\.\d{0,2})?$/.test(text)){
                                      this.setState({obj_price});
                                      return;
                                  }

                                  this.setState({obj_price:text});

                              }}
                              placeholderTextColor={'#B5B5B5'}
                          />
                      </View>
                      <TouchableOpacity disabled={this.state.btnDisabled} onPress={this._submitAction.bind(this)} style={styles.submitActionPhoneBtn}>
                          <Text style={styles.submitActionText}>确定</Text>
                      </TouchableOpacity>
                  </View>
              </ScrollView>
              <ToastTip ref="toast"/>
              <LoadingTip show={this.state.showLoading}/>
          </View>
      );
    }
}

var connectHelper = require('../../services/connectHelper');
InputGoodsPrice = connectHelper(state => state)(InputGoodsPrice);

module.exports = InputGoodsPrice;
